| Conditions | 10 |
| Paths | 36 |
| Total Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Request.performRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* jshint -W100, -W071 */ |
||
| 128 | Request.prototype.performRequest = function(options) { |
||
| 129 | var self = this; |
||
| 130 | var method = options.method; |
||
| 131 | var signHMAC = false; |
||
| 132 | |||
| 133 | if (options.auth === 'http-signature') { |
||
| 134 | signHMAC = true; |
||
| 135 | delete options.auth; |
||
| 136 | } |
||
| 137 | |||
| 138 | var uri = (self.https ? 'https://' : 'http://') + options.hostname + options.path; |
||
| 139 | |||
| 140 | var request = superagent(method, uri); |
||
| 141 | |||
| 142 | if (self.payload && (method === 'DELETE' || method === 'POST' || method === 'PUT' || method === 'PATCH')) { |
||
| 143 | request.send(self.payload); |
||
| 144 | } |
||
| 145 | |||
| 146 | _.forEach(options.headers, function(value, header) { |
||
| 147 | request.set(header, value); |
||
| 148 | }); |
||
| 149 | |||
| 150 | if (signHMAC) { |
||
| 151 | if (!self.apiSecret) { |
||
| 152 | var error = new Error("Missing apiSecret! required to sign POST requests!"); |
||
| 153 | self.deferred.reject(error); |
||
| 154 | return self.callback(error); |
||
| 155 | } |
||
| 156 | |||
| 157 | request.use(superagentHttpSignature({ |
||
| 158 | headers: ['(request-target)', 'content-md5'], |
||
| 159 | algorithm: 'hmac-sha256', |
||
| 160 | key: self.apiSecret, |
||
| 161 | keyId: self.apiKey |
||
| 162 | })); |
||
| 163 | } |
||
| 164 | |||
| 165 | request.end(function(error, res) { |
||
| 166 | var body; |
||
| 167 | |||
| 168 | if (error) { |
||
| 169 | self.deferred.reject(error); |
||
| 170 | return self.callback(error); |
||
| 171 | } |
||
| 172 | |||
| 173 | debug('response status code: %s content type: %s', res.status, res.headers['content-type']); |
||
| 174 | if (!error && (res.headers['content-type'].indexOf('application/json') >= 0)) { |
||
| 175 | try { |
||
| 176 | body = JSON.parse(res.text); |
||
| 177 | } catch (e) { |
||
| 178 | error = e; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | if (!body) { |
||
| 183 | body = res.text; |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!error && res.status !== 200) { |
||
| 187 | error = Request.handleFailure(res.text, res.statusCode); |
||
| 188 | } |
||
| 189 | |||
| 190 | if (error) { |
||
| 191 | self.deferred.reject(error); |
||
| 192 | } else { |
||
| 193 | self.deferred.resolve(body); |
||
| 194 | } |
||
| 195 | |||
| 196 | return self.callback(error, body); |
||
| 197 | }); |
||
| 198 | |||
| 199 | return self.deferred; |
||
| 200 | }; |
||
| 201 | |||
| 246 |